package persistencia.daos;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Properties;

import persistencia.consultas.Consultas;
import logica.Avion;
import logica.Blindado;
import logica.Drone;
import logica.Obstaculo;
import logica.Personaje;
import logica.Piloto;

public class DAOObstaculos {
	
	private int idEscenario;

	public DAOObstaculos() {
		super();
	}

	public DAOObstaculos(int idEs) {
		idEscenario = idEs;
	}
	
	/**
	 * Obtiene los objetos definidos en un escenario primario
	 * @param idEs
	 * @return
	 * @throws Exception
	 */
	public ArrayList<Obstaculo> obtenerObstaculosDefinidos(int idEs) throws Exception{
		ArrayList<Obstaculo> lista = new ArrayList<Obstaculo>();
		Consultas con = new Consultas();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try{
			
			Properties props = new Properties();
			String nomArch = "project.properties";
			props.load(new FileInputStream(nomArch));
			conn = DriverManager.getConnection(props.getProperty("url"), 
					props.getProperty("usr"), props.getProperty("pass"));
			
			ps = conn.prepareStatement(con.obtenerObjetosEscenarioPrimario());
			ps.setInt(1, idEs);
			
			rs = ps.executeQuery();

			while(rs.next()){
				Obstaculo obs = new Obstaculo(rs.getInt("idObjeto"), 
						rs.getInt("coordenadaX"), rs.getInt("coordenadaY"), 
						rs.getInt("alturaObjeto"), rs.getInt("anchoObjeto"), 
						rs.getInt("tipoObstaculo"), rs.getBoolean("impactable"),
						rs.getBoolean("rompible"));
				lista.add(obs);
			}
			
			//HAGO UNA CONSULTA CON EL id
			/*Obstaculo ob1 = new Obstaculo(1, 300, 50, 1, 1, 1, true, true);
			Obstaculo ob2 = new Obstaculo(1, 100, 200, 1, 1, 1, true, true);
			
			lista.add(ob1);
			lista.add(ob2);*/
		}catch(Exception e){
			throw e;
		}finally{
			if(rs != null)
				rs.close();
			if(ps != null)
				ps.close();
			if(conn != null)
				conn.close();
		}
			
		return lista;
	}
	
	/**
	 * Obtiene el obstaculo correspondiente al id de objeto ingresado por parametro
	 * @param idObstaculo
	 * @return
	 * @throws Exception
	 */
	public Obstaculo find(int idObstaculo) throws Exception{
		Obstaculo obs = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Connection conn = null;
		Consultas consultas = new Consultas();
		Properties p = new Properties();
				
		try {
			p.load(new FileInputStream("project.properties"));
			conn = DriverManager.getConnection(p.getProperty("url"), 
					p.getProperty("usr"), p.getProperty("pass"));
			ps = conn.prepareStatement(consultas.obtenerObstaculo());
			ps.setInt(1, idObstaculo);
			
			rs = ps.executeQuery();
			
			if(rs.next()){
				if(rs.getInt("tipoObjeto") == 2){//es obstaculo
					obs = new Obstaculo(rs.getInt("idObjeto"), rs.getFloat("coordenadaX"), 
							rs.getFloat("coordenadaY"), rs.getFloat("alturaObjeto"), 
							rs.getFloat("anchoObjeto"), rs.getInt("tipoObstaculo"), 
							rs.getBoolean("impactable"), rs.getBoolean("rompible"));
				}
				else{
					throw new Exception("El objeto no es un obstaculo");
				}
				
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new Exception(e);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new Exception(e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new Exception(e);
		} finally{
			if(conn != null){conn.close();}
			if(rs != null){rs.close();}
			if(ps != null){ps.close();}
		}

		return obs;
	}
	
	public void insertObstaculosActuales(int idPartida, Obstaculo o) throws Exception{
		PreparedStatement ps = null;
		ResultSet rs = null;
		Connection conn = null;
		Consultas consultas = new Consultas();
		Properties p = new Properties();
		int ultimoId = 0;
		
		try {
			p.load(new FileInputStream("project.properties"));
			conn = DriverManager.getConnection(p.getProperty("url"), p.getProperty("usr"), p.getProperty("pass"));
			
			ps = conn.prepareStatement(consultas.insertObjetosActuales());
			ps.setInt(1, idPartida);
			ps.setFloat(2, o.getId());
			
			ps.executeUpdate();
			
	
		} catch (SQLException e) {
			e.printStackTrace();
			throw new Exception(e);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new Exception(e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new Exception(e);
		} finally{
			if(conn != null){conn.close();}
			if(rs != null){rs.close();}
			if(ps != null){ps.close();}
		}
		
	}

}
